/**
 *  Copyright (C) 2010  Thomas Volk
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.bookmarks.web.pages;

import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.util.WildcardListModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.bookmarks.core.beans.Webpage;
import com.googlecode.bookmarks.core.service.ResourceService;
import com.googlecode.bookmarks.web.panels.BookmarkActionsPanel;

@AuthorizeInstantiation("USER")
public class HomePage extends AbstractMainPage {
	@SpringBean
	private ResourceService resourceService;
	private WildcardListModel<Webpage> webpages;

	public HomePage() {
		webpages = new WildcardListModel<Webpage>();
		webpages.setObject(resourceService.getAllWebpages());
		add(new ListView("bookmarks", webpages) {
			@Override
			protected void populateItem(ListItem item) {
				final Webpage page = (Webpage) item.getModelObject();
				ExternalLink link = new ExternalLink("url", page.getUrl()
						.toString());
				item.add(link);
				link.add(new Label("title", page.getTitle()));
				item.add(new Link("edit") {
					@Override
					public void onClick() {
						setResponsePage(new EditBookmarkPage(page));
					}
				});
				item.add(new Link("delete") {
					@Override
					public void onClick() {
						resourceService.deleteResource(page.getId());
						webpages.setObject(resourceService.getAllWebpages());
					}
				});
			}
		});
	}

	@Override
	protected BookmarkActionsPanel createActionPannel() {
		return new BookmarkActionsPanel("actions");
	}
}
